﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;

namespace Presentation.Views.CustomControls
{
    class FixedWidthColumn: GridViewColumn
    {
        public double FixedWidth
        {
            get { return (double)GetValue(FixedWidthProperty); }
            set { SetValue(FixedWidthProperty, value); }
        }

        public static readonly DependencyProperty FixedWidthProperty = DependencyProperty.Register
                (
                    "FixedWidth",
                    typeof(double),
                    typeof(FixedWidthColumn),
                    new FrameworkPropertyMetadata(double.NaN,
                                            new PropertyChangedCallback(OnFixedWidthChanged))
                );

        static FixedWidthColumn() 
        {
                WidthProperty.OverrideMetadata
                    (
                        typeof(FixedWidthColumn),
                        new FrameworkPropertyMetadata(null, 
                                                      new CoerceValueCallback(OnCoerceWidth))
                    );
        }
 
          
        private static object OnCoerceWidth(DependencyObject o, object baseValue) 
        {
            FixedWidthColumn fwc = o as FixedWidthColumn;
            
            if (fwc != null)
                return fwc.FixedWidth;
            
            return baseValue;
        }

        private static void OnFixedWidthChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            FixedWidthColumn fwc = o as FixedWidthColumn;
            if (fwc != null)
                fwc.CoerceValue(WidthProperty);
        }
    }
}
